package com.Cookie;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

public class Cservlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Cookie cookie = new Cookie("last",new Date().toString().replaceAll(" ","-"));

        resp.addCookie(cookie);


        String s="您是首次访问本站";
        Cookie[] cookies = req.getCookies();
        if (cookies!=null){
            for (Cookie cook:cookies) {
                if (cook.getName().equals("last")){
                    System.out.println(cook.getValue());
                    s="您的上次访问时间为"+cook.getValue();
                }
            }
        }
        resp.getWriter().println(s);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
